/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.leospa.main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.Timer;

import com.leospa.bean.Command_0;
import com.leospa.clients.SerialClient;
import com.leospa.properties.ColorPropertiesFile;
import com.leospa.properties.ConfigPropertiesFile;
import com.leospa.properties.ContentPropertiesFile;
import com.leospae.handler.Command_0_handler;

/**
 *
 * @author ADMIN
 */
public class LeoSpaMainFrame implements ActionListener {

	public Timer timer = new Timer(1, this);
	DateFormat dateFormat;
	DateFormat dateFormat1;
	Properties prop = new Properties();
	InputStream input = null;
	MainScreen mainScreen;
	javax.swing.GroupLayout layout;

	public LeoSpaMainFrame(MainScreen mainScreen, javax.swing.GroupLayout layout) {
		this.layout = layout;
		this.mainScreen = mainScreen;		
//		try {
//			URL imgURL = new File( "./images/backgrond.png" ).toURI().toURL();
//			mainScreen.setContentPane(new JLabel(new ImageIcon(imgURL)));
//		} catch (MalformedURLException e) {
//			// TODO Auto-generated catch block
//			if(ConfigPropertiesFile.DEBUG_MODE_ON == true){
//		}
		initComponents();
		jPanel1.setBackground(new Color(0, 0, 0, 0));
		jPanel1.setOpaque(false);
		timer.start();
		showDay();
		showFullDay();
		//resetRelaySwitchs();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */

	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		lblMassage = new javax.swing.JLabel();
		timeObj = new javax.swing.JLabel();
		timeAmPmObj = new javax.swing.JLabel();
		dayObj = new javax.swing.JLabel();
		fullDayObj = new javax.swing.JLabel();
		button = new javax.swing.JButton();

		jPanel1.setPreferredSize(new java.awt.Dimension(800, 1280));

		lblMassage.setFont(new java.awt.Font("SansSerif", 1, 125)); // NOI18N
		Color color = Color.decode(ColorPropertiesFile.Massage_Color);
		lblMassage.setForeground(color);
		lblMassage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		lblMassage.setText(ContentPropertiesFile.MASSAGE);

		timeObj.setFont(new java.awt.Font("SansSerif", 0, 170)); // NOI18N
		color = Color.decode(ColorPropertiesFile.Time_Text_Color);
		timeObj.setForeground(color);
		timeObj.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

		timeAmPmObj.setFont(new java.awt.Font("SansSerif", 0, 55)); // NOI18N
		color = Color.decode(ColorPropertiesFile.Time_Text_Color);
		timeAmPmObj.setForeground(color);
		timeAmPmObj.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

		color = Color.decode(ColorPropertiesFile.Day_BG_Color);
		dayObj.setBackground(color);
		dayObj.setFont(new java.awt.Font("SansSerif", 0, 50)); // NOI18N
		color = Color.decode(ColorPropertiesFile.Day_Text_Color);
		dayObj.setForeground(color);
		dayObj.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		dayObj.setMaximumSize(new java.awt.Dimension(257, 68));
		dayObj.setMinimumSize(new java.awt.Dimension(257, 68));
		dayObj.setOpaque(true);

		fullDayObj.setFont(new java.awt.Font("SansSerif", 0, 80)); // NOI18N
		color = Color.decode(ColorPropertiesFile.Date_Text_Color);
		fullDayObj.setForeground(color);
		fullDayObj.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

		URL imgURL;
		try {
			imgURL = new File( "./images/10.png" ).toURI().toURL();
			button.setIcon(new javax.swing.ImageIcon(imgURL)); // NOI18N
		} catch (MalformedURLException e) {
			if(ConfigPropertiesFile.DEBUG_MODE_ON == true){e.printStackTrace();}
		}
		button.setContentAreaFilled(false);
		button.setOpaque(false);
		button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMassage, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                             //   .addGap(10, 10, 10)
                                .addComponent(fullDayObj, javax.swing.GroupLayout.PREFERRED_SIZE, 800, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(200, 200, 200)
                                        .addComponent(dayObj, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(100, 100, 100)
                                        .addComponent(timeObj, javax.swing.GroupLayout.DEFAULT_SIZE, 600, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                .addComponent(timeAmPmObj, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 118, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(button)
                .addGap(244, 244, 244))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(122, Short.MAX_VALUE)
                .addComponent(lblMassage, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(53, 53, 53)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(timeObj, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(timeAmPmObj, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 370, Short.MAX_VALUE)
                .addComponent(dayObj, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(75, 75, 75)
                .addComponent(fullDayObj, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(button)
                .addGap(41, 41, 41))
        );

      
	}// </editor-fold>//GEN-END:initComponents
	
	public void setLayout(){
		mainScreen.getContentPane().removeAll();		
		 layout = new javax.swing.GroupLayout(mainScreen.getContentPane());
		 mainScreen.getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 815, Short.MAX_VALUE)
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	        );

	        mainScreen.getContentPane().repaint();
	        mainScreen.validate();
	        mainScreen.pack();
	        
	        if(ConfigPropertiesFile.ALWAYS_ON_TOP == true){
	        	 mainScreen.setAlwaysOnTop(true);
	        }
	        //Updated by Vaibhav to resize the screen
	        Dimension size = mainScreen.getSize(); 
			Boolean maximized = mainScreen.getExtendedState() == JFrame.MAXIMIZED_BOTH; 
			mainScreen.setSize(new Dimension((int)size.getWidth()-1,(int)size.getHeight()-1));
			if (maximized)
				mainScreen.setExtendedState(JFrame.MAXIMIZED_BOTH ); 
			else 
				mainScreen.setSize(size);
	}
	
	private void buttonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_buttonActionPerformed

		// select the Option Screen

		// if(CellModel.debug == true){
		// this.dispose();
		// new Option1().setVisible(true);
		// } else {
		/*Hashtable command0_has = new Command_0_handler().getCommand0(mainScreen,layout);
		if(command0_has != null && command0_has.size() >0){
			Util.getUtilInstance().setCommand_0_hash(command0_has);
			jPanel1.setVisible(false);
			OptionScreen optionScreen = new OptionScreen(mainScreen,layout);
			optionScreen.setLayout();
		}*/
		
		jPanel1.setVisible(false);
		LoadingMainScreen loadingMainScreen=new LoadingMainScreen(mainScreen, layout);
		loadingMainScreen.setLayout();

	}// GEN-LAST:event_buttonActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed" desc=" Look and feel setting
		// code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.
		 * html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(LeoSpaMainFrame.class.getName()).log(java.util.logging.Level.SEVERE,
					null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(LeoSpaMainFrame.class.getName()).log(java.util.logging.Level.SEVERE,
					null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(LeoSpaMainFrame.class.getName()).log(java.util.logging.Level.SEVERE,
					null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(LeoSpaMainFrame.class.getName()).log(java.util.logging.Level.SEVERE,
					null, ex);
		}
		// </editor-fold>
//		new LeoSpaMainFrame().setVisible(true);
		/* Create and display the form */
//		java.awt.EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				new LeoSpaMainFrame().setVisible(true);
//			}
//		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton button;
	private javax.swing.JLabel dayObj;
	private javax.swing.JLabel fullDayObj;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JLabel lblMassage;
	private javax.swing.JLabel timeAmPmObj;
	private javax.swing.JLabel timeObj;
	// End of variables declaration//GEN-END:variables

	@Override
	public void actionPerformed(ActionEvent e) {

		if(ConfigPropertiesFile.TIME_FORMAT == 12){
			dateFormat = new SimpleDateFormat("hh:mm a");
			Date date = new Date();
			String sa = dateFormat.format(date);
			String[] divf = sa.split("/s");
			String s1 = divf[0];
			String s2 = divf[1];
			timeObj.setText(s1);
			timeAmPmObj.setText(s2);
			if (sa.equals("12:00 AM")) {
				showDay();
				showFullDay();
			}
		} else {
			dateFormat = new SimpleDateFormat("HH:mm");
			Date date = new Date();
			String sa = dateFormat.format(date);			
			timeObj.setText(sa);
			timeAmPmObj.setVisible(false);
			if (sa.equals("00:00")) {
				showDay();
				showFullDay();
			}
		}
		

	}

	public void showFullDay()
	{
		String s1, s2;
		dateFormat = new SimpleDateFormat(ConfigPropertiesFile.DATE_FORMAT);
		Date date = new Date();
		String sa = dateFormat.format(date);
		if(ConfigPropertiesFile.DEBUG_MODE_ON == true){System.err.println("sa" + sa);}
		//#dd MMMM yyyy or yyyy MMMM dd or MMMM dd yyyy    
		
		if(ConfigPropertiesFile.DATE_FORMAT.equals("dd MMMM yyyy")){
			String datSp[] = sa.split(" ");
			String month = ContentPropertiesFile.monthHash.get(datSp[1]);
			String finalDate = datSp[0]+" "+month+" "+datSp[2];
			fullDayObj.setText(finalDate);
		} else if(ConfigPropertiesFile.DATE_FORMAT.equals("yyyy MMMM dd")){
			String datSp[] = sa.split(" ");
			String month = ContentPropertiesFile.monthHash.get(datSp[1]);
			String finalDate = datSp[0]+" "+month+" "+datSp[2];
			fullDayObj.setText(finalDate);
		} else {
			String datSp[] = sa.split(" ");
			String month = ContentPropertiesFile.monthHash.get(datSp[0]);
			String finalDate = month+" "+datSp[1]+" "+datSp[2];
			fullDayObj.setText(finalDate);
		}
		
		/*
		 * String[] divf=sa.split("/s"); s1=divf[0]; s2=divf[1];
		 * 
		 * try {
		 * 
		 * input = new FileInputStream("D:/leospaproperty/leospa.properties");
		 * // load a properties file prop.load(input); String
		 * s3=prop.getProperty(s2); fullDayObj.setText(s3); } catch (IOException
		 * ex) { if(ConfigPropertiesFile.DEBUG_MODE_ON == true){ System.out.println("hhasdadahddddhhh"+ex); } finally { if
		 * (input != null) { try { input.close(); } catch (IOException e) {
		 * if(ConfigPropertiesFile.DEBUG_MODE_ON == true){ } } }
		 */
	}

	public void showDay() {
		String s1;
		dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
		Date date = new Date();
		String sa = dateFormat.format(date);
		String[] divf = sa.split(",");
		s1 = divf[0];

		String s2 = ContentPropertiesFile.dayHash.get(s1);
		dayObj.setText(s2);

	}
	

	

}
